package com.xm.xmapp;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Reset_user_pinRequest;
import com.xm.xmapp.request.bean.Send_sms_verifcation_codeRequest;
import com.xm.xmapp.tools.Des4;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.TimeUtil;
import com.xm.xmapp.tools.TimeUtil.TimeListener;
import com.xm.xmapp.tools.Tools;

public class SettingPingActivity extends BaseActivity implements OnClickListener {

	private TimeUtil task;
	private TextView getyanzhengma;
	private EditText yanzhengma, mima, mima2;
	private TextView queren;
	private TimeListener timeListener;
	private int time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_password_ping_setting);
	}

	@Override
	public void initView() {
		getyanzhengma = (TextView) findViewById(R.id.getyanzhengma);
		yanzhengma = (EditText) findViewById(R.id.yanzhengma);
		mima = (EditText) findViewById(R.id.mima);
		mima2 = (EditText) findViewById(R.id.mima2);
		queren = (TextView) findViewById(R.id.queren);
	}

	@Override
	public void initData() {

		setMyTitle("找回用车密码");
		task = new TimeUtil(60 * 1000, 1000);
		time = 60;
		timeListener = new TimeListener() {

			@Override
			public void timeOnTick(long time0) {

				time--;
				getyanzhengma.setText(time + "秒");
			}

			@Override
			public void timeFinish() {

				getyanzhengma.setText("重新获取");
				getyanzhengma.setClickable(true);
				time = 60;
				getyanzhengma.setBackgroundResource(R.drawable.myborder1);
			}
		};

		task.setTimeListener(timeListener);
	}

	@Override
	public void initEvent() {

		queren.setOnClickListener(this);
		getyanzhengma.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.queren:
			if (StringTools.isLength(mima)) {
				if (mima.getText().toString().equals(mima2.getText().toString())) {
					if (StringTools.isLength(yanzhengma.getText().toString(), 4)) {
						reset_user_pinRequest();
					} else {
						Tools.showMessage("验证码为4到6位");
					}

				} else {
					Tools.showMessage("两次输入不同");
				}
			} else {
				Tools.showMessage("用车密码必须为6位数字");
			}

			break;
		case R.id.getyanzhengma:
			send_sms_verifcation_codeRequest();
			break;
		default:
			break;
		}
	}

	private void reset_user_pinRequest() {

		Reset_user_pinRequest requestBean = new Reset_user_pinRequest();
		requestBean.user_pin = Des4.encode(mima.getText().toString());

		requestBean.code = yanzhengma.getText().toString();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				if (response.equals("true")) {
					Tools.showMessage("修改成功");
					finish();
				} else {
					Tools.showMessage("修改失败");
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	private void send_sms_verifcation_codeRequest() {

		Send_sms_verifcation_codeRequest requestBean = new Send_sms_verifcation_codeRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				getyanzhengma.setClickable(false);
				getyanzhengma.setBackgroundResource(R.drawable.myborder4);
				task.start();

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

}
